package com.qfedu.aunt.commons.utils;

import com.alibaba.fastjson.JSON;
import com.qfedu.aunt.commons.result.R;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author xiaobobo
 * @title: ResponseUtils
 * @projectName aunt_commons_2209
 * @description: TODO
 * @date 2022/10/31  9:09
 */
@Slf4j
public class ResponseUtils {


    /**
     * 向客户端写回数据
     * @param resp
     */
    public static void out(HttpServletResponse resp, R error){
        resp.setContentType("text/html;charset=utf-8");
        //这个写完之后、接下来就是我们的写数据
        String jsonString = JSON.toJSONString(error);
        PrintWriter writer = null;
        try {
            writer = resp.getWriter();
            writer.write(jsonString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("向客户端写回数据异常....");
        }

    }



}
